/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Files;

import General.CurrentData;
import com.thoughtworks.xstream.XStream;
import other.Wizard;
import other.World;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;
import java.util.Vector;

/**
 *
 * @author David
 */
public class Project implements Serializable
{

    private File directory;
    private File assetsFolder;
    private String name;
    private World world;

    /**
     *
     * @param directoryPath
     * @param projectName
     */
    public Project(String directoryPath, String projectName)
    {
        world = new World();
        directory = new File(directoryPath + "//" + projectName);
        assetsFolder = new File(directory.getAbsolutePath() + "//assets");
        name = projectName;
    }

    /**
     *
     * @return
     */
    public File getDirectory()
    {
        return directory;
    }

    /**
     *
     * @param directory
     */
    public void setDirectory(File directory)
    {
        this.directory = directory;
    }

    /**
     *
     * @return
     */
    public File getAssetsFolder()
    {
        return assetsFolder;
    }

    /**
     *
     * @param assetsFolder
     */
    public void setAssetsFolder(File assetsFolder)
    {
        this.assetsFolder = assetsFolder;
    }

    /**
     *
     * @return
     */
    public String getName()
    {
        return name;
    }

    /**
     *
     * @param name
     */
    public void setName(String name)
    {
        this.name = name;
    }

    /**
     *
     * @return
     */
    public World getWorld()
    {
        return world;
    }

    /**
     *
     * @param world
     */
    public void setWorld(World world)
    {
        this.world = world;
    }

    /**
     *
     */
    public void create()
    {
        directory.mkdir();
        assetsFolder.mkdir();
        Wizard.saveFile(directory.getAbsolutePath() + "//" + name + ".xml", new XStream().toXML(new World()));
        Wizard.saveFile(directory.getAbsolutePath() + "//" + name + ".b3dp", this);
        setWorld(world);
    }

    /**
     *
     */
    public void getYourShitTogether()
    {
        // System.out.println(directory.getAbsolutePath() + "//" + name + ".xml");
        BufferedReader br = null;
        try
        {
            br = new BufferedReader(new FileReader(directory.getAbsolutePath() + "//" + name + ".xml"));
        } catch (FileNotFoundException ex)
        {
            CurrentData.getObserverDialog().printError("Error loading file " + directory.getAbsolutePath() + "//" + name + ".xml", ex);
        }
        StringBuilder buff = new StringBuilder();
        String line;
        try
        {
            while ((line = br.readLine()) != null)
            {
                buff.append(line);
            }
        } catch (IOException ex)
        {
            CurrentData.getObserverDialog().printError("Error reading file " + directory.getAbsolutePath() + "//" + name + ".xml", ex);
        }
        setWorld((World) new XStream().fromXML(buff.toString().substring(7/*<- because of that freakin bug of XStream, putting shit in there!!!*/)));
    }

    /**
     *
     */
    public void save()
    {
        Wizard.saveFile(directory.getAbsolutePath() + "//" + name + ".xml", new XStream().toXML(world));
        Wizard.saveFile(directory.getAbsolutePath() + "//" + name + ".b3dp", this);
    }

    public void writeFromScene(Vector<B3D_Elements.B3D_Element> elements)
    {
        XStream xStream = new XStream();
        String world = new String();
        for (B3D_Elements.B3D_Element element : elements)
        {
            System.out.println("\n");
            System.out.println(xStream.toXML(element));
            System.out.println("\n");
            world = world + "\n" + element;
        }
        Wizard.saveFile("world.xml", xStream.toXML(world));
    }
}
